<?php
namespace Home\Controller;
use Think\Controller;
class GoodsController extends Controller{
	public function index(){
		$index = D('Type');
		//分配数据 遍历导航栏
	    $typelist = $index->getType();
	    $this->assign('pid',$typelist);
		$this->display();
	}

	public function detail(){
		$index = D('Type');
		//分配数据 遍历导航栏
	    $typelist = $index->getType();
	    $this->assign('pid',$typelist);
	    $id = $_GET['id'];
	    //三级分类名
	    $name = $index->table(array('type'=>'t','goods'=>'g'))->field('t.id,g.*')->where("t.id=g.typeid AND $id=g.id")->getField('name');
	    //二级分类名
		 $name1 = $index->table(array('type'=>'t','goods'=>'g'))->field('t.id,t.pid,g.*')->where("t.id=g.typeid AND $id=g.id")->getField('pid');
		 $name2 = $index->query("select * from type where id=$name1");
		//一级分类

		
		//商品信息
		$goods = D('Goods');
		$id = $_GET['id'];
		$result = $goods->where("id="."{$id}")->select();
		
		$this->assign('name',$name);
		$this->assign('name2',$name2);
		$this->assign('list',$result);

		//放大镜
		$img = D('Goods_img');
		$map['goodsid'] = $_GET['id'];
		$index = $img->where($map)->limit(0,4)->select();
		$this->assign('img',$index);

		//详情图片
		$detail = D('Goods_detail');
		$map['goodsid'] = $_GET['id'];
		$detaillist = $detail->where($map)->limit(0,4)->select();

//---------------
//Author:Hua
//--------------
		/*详情页遍历该商品的评论*/
        $com = D('Comments');
        //$goods = D('Goods');
        $user = D('User');
        $dis = $com->where($map)->select();
        $count = $com->where($map)->count();
        
        $level = array('好评','中评','差评');
        foreach($dis as $key=>$val){
        	$dis[$key]['picname'] = $goods->where("id=".$val['goodsid'])->find()['picname'];
        	$dis[$key]['username'] = $user->where("id=".$val['userid'])->find()['username'];
        	$dis[$key]['photo'] = $user->where("id=".$val['userid'])->find()['photo'];

        	$dis[$key]['clevel'] = $level[$val['clevel']];	
			$time=$dis[$key]['ctime'];
			$truetime=date('Y-m-d H:i:s',$time);
			$dis[$key]['ctime'] =$truetime;
        }
        if(empty($dis))$dis[]['sg']=1;
        //dump($dis);
        $this->assign('dis',$dis);
        $this->assign('count',$count);

		$this->assign('detail',$detaillist);
		$this->display();
	}

	public function Goodslist(){
		

		$goods = D('Goods');
        $search = $_GET['keyword'];
    	
    	$page = new \Think\Page($total,10);
    	$pageButton = $page->show();
        if($search){
            $arr['descr'] = array('like',"%{$search}%");
            $total = $goods->where($arr)->count();
    	    $goodslist = $goods->limit($page->firstRow.','.$page->listRows)->query("select  * from goods where typeid in(select id from type where path like '%,{$_GET['id']},%') and descr like '%{$search}%'");
        }else{
            $total = $goods->count();
            $goodslist = $goods->limit($page->firstRow.','.$page->listRows)->query("select  * from goods where typeid in(select id from type where path like '%,{$_GET['id']},%')");
        }
    	$type = D('Type');
    	$name = $type->where('id='.$_GET[id])->getField('name');
    	
    	$typelist = $type->getType();
	    $this->assign('pid',$typelist);
    	$this->assign('name',$name);
    	$this->assign('total',$total);
    	$this->assign('pageButton',$pageButton);
    	$this->assign('list',$goodslist);
    	$this->display();

	}

	
}

?>